<template>
  <div>

    <div style="margin-top: 200px ;margin-left: 300px; width: 400px;height: 400px;">
      <el-form ref="form" :model="user" label-width="80px">

        <el-form-item label="用户名：">
          <el-input style="width: 260px" v-model="user.userName" :disabled="true"></el-input>
        </el-form-item>

        <el-form-item label="密码：">
          <el-input style="width: 260px" v-model="user.userPassword" :type="inputStatus" :disabled="true"></el-input>
        </el-form-item>

      </el-form>
      <div style="margin-left: 80px">
        <el-button size="small" type="primary" @click="check" plain>查看密码</el-button>
        <el-button size="small" type="warning" @click="dialogVisible = true;" plain>修改密码</el-button>
        <el-button size="small" type="danger" @click="exitSys" plain>退出系统</el-button>
      </div>
    </div>


    <el-dialog
        title="修改密码"
        :visible.sync="dialogVisible"
        width="30%"
        @close='closeDialog'
    >
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "LoginSettings",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      //是否显示弹框
      dialogVisible: false,
      //切换输入框的状态
      inputStatus: 'password',
      //用户的信息
      user: {
        userId: '',
        userName: '',
        userPassword: ''
      },
      ruleForm: {
        pass: '',
        checkPass: '',
        age: ''
      },
      rules: {
        pass: [
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ]
      }
    };
  },
  mounted: function () {
    this.selectUser();
  },
  methods: {
    /* 关闭新建清空表单 */
    closeDialog() {
      location.reload();
    },
    //查看密码
    check() {
      this.inputStatus = 'text'
    },
    //检查字符串是否有空字符
    examine() {
      for (let i = 0; i < this.ruleForm.checkPass.length; i++) {
        // console.log(this.ruleForm.checkPass[i]);
        if (this.ruleForm.checkPass[i] == " ") {
          return "1";
        }
      }
      return "2";
    },
    //修改密码弹框提交
    submitForm() {
      var that = this;
      // console.log(this.ruleForm.checkPass);
      // console.log(this.ruleForm.pass);
      if (this.examine() == '2') {
        if (this.ruleForm.checkPass != this.ruleForm.pass) {
          that.$message.error("请按照正确的格式修改密码！")
        } else {
          this.$http.put("/users/updateById/" + this.user.userId + "/" + this.ruleForm.checkPass)
              .then(function (response) {
                // console.log(response);
                if (response.data.code === '200') {
                  that.$message.success("修改成功，请使用新的密码登录！");
                  that.dialogVisible = false;
                  //修改完成后，重新登录
                    that.exitSys();
                } else {
                  that.$message.error("修改失败，请稍后重试！");
                }
              }).catch(function (error) {
            console.log(error);
            that.$message.error("请按照正确的格式修改密码！");
          })
        }
      } else {
        that.$message.error("请按照正确的格式修改密码,密码中不能有空字符！");
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //查询用户名密码
    selectUser() {
      var that = this;
      this.user.userName = this.$route.query.userName;
      // console.log(this.$route.query.userName)
      this.$http.post("/users/selectUser", this.user)
          .then(function (response) {
            // console.log(response);
            that.user.userPassword = response.data.data.userPassword;
            that.user.userId = response.data.data.userId;
          }).catch(function (error) {
        console.log(error);
      });
    },
    //退出系统
    exitSys() {
      // 清空token
      window.sessionStorage.clear();
      // 跳转到登录页
      this.$router.push("/LoginSys");
    }
  }
};
</script>

<style scoped>

</style>